package com.qfedu.edu.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

/**
 * @author xiaobobo
 * @title: SecurityConfig2
 * @projectName CD-Java-FY-2401-Springboot-Parent
 * @description: 这里就要开始配置我们的Security框架了
 * @date 2024/9/12  10:36
 */
@SpringBootConfiguration
public class SecurityConfig2 extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //关闭防止跨站点伪造
        http.csrf().disable();

        //在这里编写RememberMe的配置
        http.rememberMe()
                .userDetailsService(userDetailsService) //设置的是UserDetailService对象
                .tokenValiditySeconds(2592000)  //token的过期时间
                .tokenRepository(persistentTokenRepository());   //设置的事token的持久化策略


        //鉴权的配置

        http.authorizeRequests()
                .antMatchers("/test1");
//                        .hasAnyAuthority()  具有任何一个权限都能访问
//                        .hasAnyRole() //具有任何一个角色都能访问
//                        .hasAuthority("user_query"); //表示具有某一个权限才能访问
//                        .hasRole("admin");  //表示具有某一个角色才能访问


        //在这里做退出的配置
        http.logout()
                .logoutUrl("/users/logout")   //这个就是退出的处理地址
                .logoutSuccessUrl("/login.html");  //这个就是退出成功之后要跳转的页面

        http.userDetailsService(userDetailsService)
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/users/login")
                .successForwardUrl("/test")
                .failureForwardUrl("/login.html")
                .usernameParameter("username")
                .passwordParameter("password");

        http.authorizeRequests()
                //这个表示的是 登陆页面赋予所有权限 直接可以访问
                .antMatchers("/login.html")
                .permitAll()
                //其余的任何请求都需要登陆之后才能访问
                .anyRequest()
                .authenticated();
    }

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder;
    }

    @Autowired
    private DataSource dataSource;

    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        jdbcTokenRepository.setCreateTableOnStartup(true);
        return jdbcTokenRepository;
    }
}
